package com.hoongs.v1.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoongs.v1.entity.Category;
import com.hoongs.v1.mapper.CategoryMapper;
import com.hoongs.v1.service.CategoryService;
import com.hoongs.v1.utils.PResult;
import com.hoongs.v1.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;

@Service
public class CategoryServiceImpl implements CategoryService {

    public static final String NOT_FOUND_ARTICLE = "没有找到该分类！";

    @Resource
    private CategoryMapper categoryMapper;

    @Override
    public Result add(Category category) {
        if (categoryMapper.insert(category) == 1) {
            return Result.opSuccess();
        } else {
            return Result.opError();
        }
    }

    @Override
    public Result remove(String id) {
        if (!findArticleById(id)) return Result.opError(NOT_FOUND_ARTICLE);
        if (categoryMapper.deleteById(id) == 1) {
            return Result.opSuccess();
        } else {
            return Result.opError();
        }
    }

    @Override
    public Result update(Category category) {
        if (!findArticleById(category.getId())) return Result.opError(NOT_FOUND_ARTICLE);
        if (categoryMapper.updateById(category) == 1) {
            return Result.opSuccess();
        } else {
            return Result.opError();
        }
    }

    @Override
    public Result selectById(String id) {
        Category category = categoryMapper.selectById(id);
        if (category == null) return Result.error(NOT_FOUND_ARTICLE);
        return Result.success(category);
    }

    @Override
    public Result selectList(String name, String status) {
        QueryWrapper<Category> wrapper = new QueryWrapper<Category>();
        if (StringUtils.isNotBlank(name)) {
            wrapper.like("name", name);
        }
        if (status != null) {
            wrapper.eq("status", status);
        }
        wrapper.orderBy(true, true, "ordercode");
        Collection<Category> list = categoryMapper.selectList(wrapper);
        return Result.success(list);
    }


    @Override
    public Result selectPageList(Integer page, Integer size, String name, String status) {
        if (!PResult.checkPageQuery(page, size)) return Result.error("分页参数错误");

        Page<Category> daoPage = new Page<>(page, size);
        QueryWrapper<Category> wrapper = new QueryWrapper<Category>();
        if (StringUtils.isNotBlank(name)) {
            wrapper.like("name", name);
        }
        if (status != null) {
            wrapper.eq("status", status);
        }
        IPage<Category> categoryDaoIPage = categoryMapper.selectPage(daoPage, wrapper);
        PResult rs = new PResult(categoryDaoIPage);
        return Result.success(rs);
    }

    /**
     * 根据Id查询文章是否存在
     */
    public Boolean findArticleById(String id) {
        Category category = categoryMapper.selectById(id);
        return category != null;
    }
}
